package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @RequestMapping("/findMenuparentMenuId")
    public Result findMenuparentMenuId(){
        try {
            List<Map<String, Object>> list = menuService.findMenuparentMenuId();
            return new Result(true, MessageConstant.QUERY_PARENT_MENU_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PARENT_MENU_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu, Integer id){
        try {
            if(id == 999999){
                id = null;
            }
            menu.setParentMenuId(id);
            menuService.add(menu);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }

    @RequestMapping("/pageQuery")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean){
        return menuService.pageQuery(queryPageBean);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    @RequestMapping("/findParentIdsByMenuId")
    public List<Integer> findParentIdsByMenuId(Integer mid){
        return menuService.findParentIdsByMenuId(mid);
    }

    @RequestMapping("/findMenuIdAndDisabled")
    public Result findMenuIdAndDisabled(Integer id){
        try {
            List<Map<String, Object>> list = menuService.findMenuIdAndDisabled(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu, Integer id){
        try {
            if(id == 999999){
                id = null;
            }
            menu.setParentMenuId(id);
            menuService.edit(menu);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            menuService.delete(id);
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        List<Menu> list= menuService.findAll();
        if(list!=null&&list.size()>0){
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,list);
        }
        return new Result(false, MessageConstant.QUERY_MENU_FAIL);
    }

}
